const api = require('../../../ws_micro_shop/utils/api.js');
const app = getApp();

let is_more = false;
Page({
  data: {
    show: false,
    unit: '',
    abnorData: {
      imgSrc: '/common/static/images/abnor-04.png',
      text: '暂无内容',
    },
  },
  onLoad(options) {
    let that = this;
    let isBrand = app.ws_micro_shop_globalData && app.ws_micro_shop_globalData.isBrand; 
    let opts = options || {};
    let date = new Date();
    let year = date.getFullYear();
    let month = date.getMonth() + 1;
    month = month.toString();
    month = month[1] ? month : '0' + month;
    this.setData({ 
      opts,
      date: year + '年' + month + '月',
      isBrand,
    });
    app.loadCurrencyUnit().then(res => {
      that.setData({
        unit: res.unit,
      });
    });
    this.getData(year + '-' +month);
  },
  getData(date) {
    let that = this;
    wx.showLoading({
      title: '加载中',
      mask: true,
    });
    app.ws_micro_shoprequest({
      url: api.user.scoredetail,
      data: {
        date,
      },
      success: function (res) {
        let resp = res.data;
        let { user_integral, list, setting } = resp;
        that.setData({
          list,
          setting,
          date_1: resp.date,
          user_integral,
        });
        wx.hideLoading();
        is_more = false;
      },
    });
  },
  dateChange(e) {
    if (is_more) {
      return;
    }
    is_more = true;
    let date_1 = e.detail.value;
    let date = date_1.replace('-', '年') + '月';
    this.setData({
      date,
      date_1,
      list: [],
    });
    this.getData(date_1);
  },
  dateUp() {
    let that = this;
    if (is_more) {
      return;
    }
    is_more = true;
    let date_1 = that.data.date_1;
    let d = new Date(date_1);
    d.setMonth(d.getMonth() + 1);
    let m = d.getMonth() + 1;
    m = m.toString();
    m = m[1] ? m : '0' + m;
    that.setData({
      date: d.getFullYear() + '年' + m + '月',
      date_1: d.getFullYear() + '-' + m,
      list: [],
    });
    that.getData(d.getFullYear() + '-' + m);
  },
  dateDown() {
    let that = this;
    if (is_more) {
      return;
    }
    is_more = true;
    let date_1 = that.data.date_1;
    let d = new Date(date_1);
    d.setMonth(d.getMonth() - 1);
    let m = d.getMonth() + 1;
    m = m.toString();
    m = m[1] ? m : '0' + m;
    that.setData({
      date: d.getFullYear() + '年' + m + '月',
      date_1: d.getFullYear() + '-' + m,
      list: [],
    });
    that.getData(d.getFullYear() + '-' + m);
  },
  click() {
    this.setData({
      show: true,
    });
  },
  close() {
    this.setData({
      show: false,
    });
  },
  onNavigate(e) {
    let param = e.currentTarget.dataset;
    app.ws_micro_shop_navigate(param);
  },
  collectFormId(e) {
    app.collectFormId(e);
  },
});
